package wu.controller;

import wu.mapper.StudentMapper;
import wu.pojo.NiuStudent;
import wu.util.MyBatisUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class ExcelGenerator {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要生成表格的周赛周数：");
        int num = sc.nextInt();
        List<NiuStudent> list2 = new ArrayList<NiuStudent>();
        StudentMapper studentMapper = MyBatisUtils.getSession().getMapper(StudentMapper.class);
        List<Integer> niuIds = studentMapper.getNiuIds();
        for (Integer niuId : niuIds) {
            NiuStudent niuStudent = studentMapper.getStudentByIdAndTableName("user_table_" + num, niuId);
            // 处理查询到的学生和相关 User_niu 信息
            if (niuStudent != null) {
                list2.add(niuStudent);
            }
        }
        // 创建一个Comparator接口实现，用于比较两个NiuStudent对象的niu_total_score属性
        Comparator<NiuStudent> comparator = new Comparator<NiuStudent>() {
            @Override
            public int compare(NiuStudent s1, NiuStudent s2) {
                if (s1.getNiu_total_score() == null) {
                    return 1;
                }
                if (s2.getNiu_total_score() == null) {
                    return -1;
                }
                // 将字符串转换为double类型后进行比较
                double score1 = Double.parseDouble(s1.getNiu_total_score());
                double score2 = Double.parseDouble(s2.getNiu_total_score());
                if (score2 > score1) {
                    return 1;
                } else if (score2 < score1) {
                    return -1;
                } else {
                    return s1.getNiu_time().compareTo(s2.getNiu_time());
                }
            }
        };
        // 使用Collections.sort方法对列表进行排序
        Collections.sort(list2, comparator);
        for (NiuStudent niuStudent : list2) { // 处理排序后的学生信息
            System.out.println(niuStudent); // 输出学生信息
        }
        String filePath = "src/main/resources/excel/研发中心牛客周赛 Round " + num + " 成绩表.xlsx";
        File file = new File(filePath);
        File parentDir = file.getParentFile();
        if (!parentDir.exists()) {
            parentDir.mkdirs();
        }
        if (!file.exists()) {
            try {
                file.createNewFile();
                System.out.println("文件创建成功");
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("文件创建失败");
                return;
            }
        }
        // 生成 Excel 文件
        boolean b = wu.service.ExcelGenerator.generateExcel(list2, filePath, num);
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println(b ? "成功创建表格   位置:src/main/resources/excel/" : "创建表格失败");
        System.out.println();


    }
}

